<?php

class WangEditor4 {
    
    public $simpleModel = false; // 模式 'default' 或 'simple'
    
    public $enableUpload = true; // 允许上传文件
    
    public $base64_upload_server = ''; // 文件上传服务器地址
    
    public $chunk_upload_server = ''; // 文件分片上传服务器地址
    
    public $remote_capture_server = ''; // 图片远程抓取服务器地址
    
    public $css_class = ''; // 样式
    
    private $values = array();
    
    private $_init = false;
    
    function create($name = '') {
        $name = "HtmlEditor{$name}";
        
        if ($this->enableUpload) {
            
            if (!$this->chunk_upload_server) {
                throw new Exception('未指定文件分片上传服务器地址');
            }
            
            if (!$this->base64_upload_server) {
                throw new Exception('未指定文件上传服务器地址');
            }
            
            if (!$this->remote_capture_server) {
                throw new Exception('未指定图片远程抓取服务器地址');
            }
        }
        
        if (!$this->_init) {
            print <<<eot
            <script type="text/javascript" src="/plugins/wangEditor/wangEditor.min.js?4.7.15"></script>
            <script type="text/javascript" src="/plugins/wangEditor/plugins/dialog-plus.js"></script>
            <script type="text/javascript" src="/plugins/webuploader/webuploader.html5only.min.js"></script>
            <script type="text/javascript" src="/plugins/wangEditor/creator.js"></script>
eot;
            $this->_init = true;
        }
        
        $enableUpload = $this->enableUpload ? 'true' : 'false';
        $simpleModel = $this->simpleModel ? 'true' : 'false';
        
        print <<<eot
            <textarea id="{$name}" name="{$name}" style="width: 100%; height: 300px; display:none;">{$this->values[$name]}</textarea>
            <div id="{$name}-editor-container" class="{$this->css_class}" style="min-hight: 300px;width: 100%;">{$this->values[$name]}</div>
            <script type="text/javascript">
                createEditor('{$name}', {
                    enableUpload: {$enableUpload},
                    simpleMode: {$simpleModel},
                    base64_upload_server: "{$this->base64_upload_server}",
                    remote_capture_server: "{$this->remote_capture_server}",
                    chunk_upload_server: "{$this->chunk_upload_server}",
                });
            </script>
eot;
    
    }
    
    /**
     * 已 htmlspecialchars，可直接存入数据库
     * @return type
     */
    public function getContent($name = '') {
        $name = "HtmlEditor{$name}";
        $content = "";
        if (isset($_POST[$name])) {
            $content = $_POST[$name];
            if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
                $content = stripslashes($content);
            }
            $content = htmlspecialchars(str_replace(array("\n", "\r", "\t"), "", $content), ENT_QUOTES);
        }
        return $content;
    }
    
    /**
     * 可传入未 htmlspecialchars_decode 的html串
     * @param type $content
     */
    public function setContent($content = '', $name = '') {
        $name = "HtmlEditor{$name}";
        $content = htmlspecialchars_decode(str_replace(array("\n", "\r", "\t"), "", $content), ENT_QUOTES);
        $pos = stripos($content, '<p>');
        
        if ($pos === false) {
            $content = '<p>' . $content . '<br /></p>';
        } else if ($pos > 0) {
            $content = '<p>' . substr($content, 0, $pos) . '<br /></p>' . substr($content, $pos);
        }
        $this->values[$name] = $content;
    }
    
}
